﻿using DevExpress.Utils.Win;
using DevExpress.XtraEditors.Popup;
using DevExpress.XtraGrid.Demos.util;
using DevExpress.XtraGrid.Editors;
using DevExpress.XtraLayout;
using DXApplicationTangche.UC.已开立看板.Service;
using DXApplicationTangche.UC.生产计划.编排生产批.service;
using DXApplicationTangche.UC.订单一览新.订单预览新;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace DXApplicationTangche.UC.已开立看板
{
    public partial class Form已开立看板 : DevExpress.XtraEditors.XtraForm
    {
        //private List<DailyOrderDTO> dailyOrderDTOs = new List<DailyOrderDTO>();
        private List<DailyOrderDisplayDTO> dailyOrderDisplayDTOs = new List<DailyOrderDisplayDTO>();

        private String pmcDate { get; set; }

        public Form已开立看板()
        {
            InitializeComponent();
            this.searchLookUpEdit选择时间.Properties.DataSource = 已开立Service.getDailyOrderLogRecentMonth();
            
        }
        private void 查询()
        {
            List<DailyOrderDTO> dailyOrderDTOs = 已开立Service.getDailyOrderByPmcDate(pmcDate);
            this.dailyOrderDisplayDTOs.Clear();
            foreach (DailyOrderDTO dailyOrderDTO in dailyOrderDTOs)
            {
                this.dailyOrderDisplayDTOs.Add(new DailyOrderDisplayDTO(dailyOrderDTO));
            }
            this.gridControl已开立看板.DataSource = this.dailyOrderDisplayDTOs;
            this.advBandedGridView已开立看板.ExpandAllGroups();
            this.advBandedGridView已开立看板.RefreshData();
        }
        #region 选择时间
        private void searchLookUpEdit选择时间_Popup(object sender, EventArgs e)
        {
            PopupSearchLookUpEditForm form = (sender as IPopupControl).PopupWindow as PopupSearchLookUpEditForm;
            SearchEditLookUpPopup popup = form.Controls.OfType<SearchEditLookUpPopup>().FirstOrDefault();
            LayoutControl layout = popup.Controls.OfType<LayoutControl>().FirstOrDefault();
            //如果窗体内空间没有确认按钮，则自定义确认simplebutton，取消simplebutton，选中结果label
            if (layout.Controls.OfType<Control>().Where(ct => ct.Name == "btOK").FirstOrDefault() == null)
            {
                //得到空的空间
                EmptySpaceItem a = layout.Items.Where(it => it.TypeName == "EmptySpaceItem").FirstOrDefault() as EmptySpaceItem;

                //得到取消按钮，重写点击事件
                Control clearBtn = layout.Controls.OfType<Control>().Where(ct => ct.Name == "btClear").FirstOrDefault();
                LayoutControlItem clearLCI = (LayoutControlItem)layout.GetItemByControl(clearBtn);
                clearBtn.Click += clearBtn_Click;

                //添加一个simplebutton控件(确认按钮)
                LayoutControlItem myLCI = (LayoutControlItem)clearLCI.Owner.CreateLayoutItem(clearLCI.Parent);
                myLCI.TextVisible = false;
            }
        }
        private void searchLookUpEdit选择时间View_Click(object sender, EventArgs e)
        {
            int[] a = this.searchLookUpEdit选择时间.Properties.View.GetSelectedRows();
            if (a.Length > 0)
            {
                foreach (int rowHandle in a)
                {
                    this.pmcDate = this.searchLookUpEdit选择时间.Properties.View.GetRowCellValue(rowHandle, "pmcDate").ToString();
                }
            }
            this.查询();
        }

        /// <summary>
        /// 清除按钮事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void clearBtn_Click(object sender, EventArgs e)
        {
            this.searchLookUpEdit选择时间.ToolTip = null;
            searchLookUpEdit选择时间.EditValue = null;
        }
        private void searchLookUpEdit选择店铺_CustomDisplayText(object sender, DevExpress.XtraEditors.Controls.CustomDisplayTextEventArgs e)
        {
            if (null != e.Value)
                e.DisplayText = this.pmcDate;
        }
        #endregion

        private void 编排生产计划ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            try
            {
                String batchNo = FunctionHelper.GetgridViewValue(
                    this.advBandedGridView已开立看板,
                    this.advBandedGridView已开立看板.FocusedRowHandle,
                    "batchNo");
                DTO生产批 dTO生产批 = 编排生产批Service.getWIPBatchByBatchNo(batchNo);
                if (String.IsNullOrEmpty(dTO生产批.orderId))
                {
                    return;
                }
                else
                {
                    new Form订单预览新(dTO生产批).ShowDialog();
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
            //重新查询
            //this.查询();
        }

        private void advBandedGridView已开立看板_CustomDrawCell(object sender, DevExpress.XtraGrid.Views.Base.RowCellCustomDrawEventArgs e)
        {
            if (e.Column.Name.Equals("gridColumn加急标识"))
            {
                if ("加急".Equals(e.CellValue.ToString()))
                {
                    e.Appearance.BackColor = Color.OrangeRed;
                }
                else
                {
                    e.Appearance.BackColor = Color.LightGreen;
                }
            }
            if (e.Column.Name.Equals("gridColumn是否逾期"))
            {
                if ("是".Equals(e.CellValue.ToString()))
                {
                    e.Appearance.BackColor = Color.OrangeRed;
                }
                else
                {
                    e.Appearance.BackColor = Color.LightGreen;
                }
            }
        }
    }
}
